package com.lbs.demo.services.MusicCrawler;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lbs.demo.entities.SongsEntity;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.ArrayList;
import java.util.List;

public class QQMusicCrawler implements PageProcessor {

    public static List<SongsEntity> topList;

    private Site site = Site.me().setRetryTimes(3).setSleepTime(200)
            .setUserAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3").setCharset("UTF-8");

    @Override
    public void process(Page page) {
        String jsonStr = page.getHtml().xpath("//body/text()").regex("\\{.*\\}").toString();
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = (JsonObject) parser.parse(jsonStr);//歌曲数组
        JsonArray jsonArray = jsonObject.get("songlist").getAsJsonArray();//歌曲数组
        //System.out.println(jsonArray.size());
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject object=jsonArray.get(i).getAsJsonObject();//每一首歌曲
            SongsEntity song = new SongsEntity();
            song.setSrc("qq");//歌曲来源
            song.setSongId(object.get("data").getAsJsonObject().get("songid").getAsString());//歌曲编号
            song.setName(object.get("data").getAsJsonObject().get("songname").getAsString());//歌曲名称
            String songSinger = "";//歌手
            for (int t = 0; t < object.get("data").getAsJsonObject().get("singer").getAsJsonArray().size(); t++) {
                if (t > 0) songSinger = songSinger.concat(" & ");
                songSinger = songSinger.concat(object.get("data").getAsJsonObject().get("singer").getAsJsonArray().get(t).getAsJsonObject().get("name").getAsString());//每一个歌手
            }
            song.setSinger(songSinger);
            song.setDuration(object.get("data").getAsJsonObject().get("interval").getAsInt()*1000);//interval
            topList.add(song);
//            System.out.println("歌曲编号:" + song.getSongId() + " 名称：" + song.getName() + " 时长：" + song.getDuration() + " 歌手：" + song.getSinger());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static List<SongsEntity> getHotMusic() {
        topList = new ArrayList<>();
        Request request = new Request("https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2017_40&topid=26&type=top&song_begin=0&song_num=100&g_tk=5381&jsonpCallback=MusicJsonCallbacktoplist&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new QQMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }

    public static List<SongsEntity> getNewMusic() {
        topList = new ArrayList<>();
        Request request = new Request("https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2017-10-10&topid=27&type=top&song_begin=0&song_num=100&g_tk=5381&jsonpCallback=MusicJsonCallbacktoplist&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new QQMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }

    public static List<SongsEntity> getPopularMusic() {
        topList = new ArrayList<>();
        Request request = new Request("https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2017-10-10&topid=4&type=top&song_begin=0&song_num=100&g_tk=5381&jsonpCallback=MusicJsonCallbacktoplist&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new QQMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }

}
